from pathlib import PurePath
from typing import Sequence

from common.common_consts import AGENT_OTP_ENVIRONMENT_VARIABLE
from common.types import AgentID
from infection_monkey.exploit import IAgentOTPProvider
from infection_monkey.model import CMD_PREFIX, DROPPER_ARG, MONKEY_ARG, SET_OTP_WINDOWS
from infection_monkey.utils.commands import build_monkey_commandline


def build_smb_command(
    agent_id: AgentID,
    servers: Sequence[str],
    current_depth: int,
    remote_agent_binary_full_path: PurePath,
    remote_agent_binary_destination_path: PurePath,
    otp_provider: IAgentOTPProvider,
) -> str:
    otp = SET_OTP_WINDOWS % {
        "agent_otp_environment_variable": AGENT_OTP_ENVIRONMENT_VARIABLE,
        "agent_otp": otp_provider.get_otp(),
    }

    if (
        str(remote_agent_binary_full_path).lower()
        != str(remote_agent_binary_destination_path).lower()
    ):
        cmdline = (
            f"{CMD_PREFIX} {otp} start cmd /c {str(remote_agent_binary_full_path)} {DROPPER_ARG}"
            + build_monkey_commandline(
                agent_id,
                servers,
                current_depth + 1,
                str(remote_agent_binary_destination_path),
            )
        )
    else:
        cmdline = (
            f"{CMD_PREFIX} {otp} start cmd /c {str(remote_agent_binary_full_path)} {MONKEY_ARG}"
            + build_monkey_commandline(agent_id, servers, current_depth + 1)
        )

    return cmdline
